/**
 * 
 */
package denaris.core;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

import denaris.core.controller.AbstractController;
import denaris.core.controller.DegressiveVectorController;
import denaris.factory.ImageFactory;

/**
 * The ExhaustSystem is a special particle system which produces small fume
 * particles on the rear end of the player sprite. The inner particle emitter
 * uses a DegressiveVectorController which gives the particles some dynamic
 * movement.
 * 
 * @author stkl23
 */
public class ExhaustSystem extends AbstractParticleSystem {
	/**
	 * a 1x1 pixel image used for the sprite which is maintained by the
	 * controller
	 */
	protected Image transparent;

	/** the different fume colors */
	private int[] fumeColor = { 0xFFFFFF, 0xFFFFFF, 0xFFFF00, 0xA0A000, 0x804000, 0x602020, 0x402020, 0x202020, 0x101010, 0x050505, 0x000000 };

	public class ExhaustParticle extends AbstractParticle{
		private int fumeColorIndex = 0;

		public ExhaustParticle(AbstractController controller) {
			super(controller);
		}

		public void paint(Graphics g) {
			if (isActive()) {
				g.setColor(fumeColor[fumeColorIndex]);
				Sprite sprite = getController().getSprite();
				g.fillRect(sprite.getX(), sprite.getY(), 1, 1);
			}
		}

		public void update() {
			getController().advance();
			fumeColorIndex++;
			if (fumeColorIndex >= fumeColor.length) {
				fumeColorIndex = 0;
				notifyOutOfScope();
			}
		}
	}
	
	public ExhaustSystem() {
		// load the image
		transparent = ImageFactory
				.getImage("/denaris/resource/sprite/transparent.png");

		createEmitters();
	}

	protected void createEmitters() {
		// create an inner particle emitter (no need to write an extra class for this)
		addEmitter(new AbstractParticleEmitter(10, -1, 6) {
			protected AbstractParticle createNew() {
				Sprite s = new Sprite(transparent, 1, 1);
				DegressiveVectorController controller = new DegressiveVectorController(-16, 0);
				controller.setSprite(s);
				return new ExhaustParticle(controller);
			}
		});
	}
	
	public void setColors(int[] colors) {
		fumeColor = colors;
	}
}
